//
//  NetWorkErrorView.swift
//  Wanke
//
//  Created by BO ZHOU on 2020/10/26.
//

import UIKit

open class NetWorkErrorView: UIView {
    
    /** 记录loadingView的索引值, 每显示一次 +1, 每hide一次 -1, 如果为0则隐藏 */
    var indexTag: Int = 0
    
    var urlArray: [String] = []
    
    var buttonClickBlock: (()->Void)?

    lazy var imageview: UIImageView = {
       
        let imageview = UIImageView()
        imageview.imageName = "无网络"
        
        return imageview
    }()
    
    
    lazy var label: UILabel = {
       
        let label = UILabel()
        label.text = "服务器开小差了，请稍后点击重试"
        label.sizeToFit()
        label.textColor = UIColor.white.withAlphaComponent(0.45)
        label.font = UIFont.mediumFont(size: 14)
        label.textAlignment = .center
        
        return label
    }()
    
    lazy var button: UIButton = {
        
        let button = UIButton()
        button.setTitle("点击重试", for: .normal)
        button.setTitleColor(.white, for: .normal)
        button.adjustsImageWhenHighlighted = false
        button.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        button.cornerRadius = 20
        
        button.clickBlock = { [unowned self] (btn) in
            self.buttonClickBlock?()
        }
        
        return button
    }()
    
    
    lazy var returnButton: UIButton = {
       
        let btn = UIButton()
        btn.image_size = CGSize(width: 14, height: 14)
        

        btn.setImage(UIImage.named_lib("fanhui"), for: .normal)
        btn.clickBlock = { [unowned self] (btn)in
            self.hideSelf()
        }
        
        btn.alpha = 0.7
        return btn
    }()
    
    
    
    public override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = UIColor.init(hexString: "#FF363A43")
        addSubview(imageview)
        addSubview(label)
        addSubview(button)
        
        addSubview(returnButton)
        
        KEY_WINDOW?.addSubview(self)
        self.frame = KEY_WINDOW!.bounds
        self.alpha = 0
    }
    
    required public init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    public override func layoutSubviews() {
        super.layoutSubviews()
        
        button.gradientColorWithHexString(hexStrings: ["#FF5273E0", "#FF3A54AA"])
        
        let totalHeight: CGFloat = 237
        let imageWidth: CGFloat = 182
        let imageHeight: CGFloat = 152
        imageview.frame = CGRect(x: self.width/2 - imageWidth/2, y: self.height/2 - totalHeight/2 - 32, width: imageWidth, height: imageHeight)
        
        label.origin = CGPoint(x: 0, y: imageview.frame.maxY + 20)
        label.width = self.width
        
        let buttonWidth: CGFloat = 160
        let buttonHeight: CGFloat = 40
        button.frame = CGRect(x: self.width/2 - buttonWidth/2, y: label.frame.maxY + 20, width: buttonWidth, height: buttonHeight)
        
        returnButton.frame = CGRect(x: 10, y: 40, width: 40, height: 40)
    }

    
    func showSelf(clickBlock: (()->Void)?) {
        
//        indexTag += 1
        
        self.frame = self.superview?.bounds ?? KEY_WINDOW!.bounds
        
        self.buttonClickBlock = clickBlock
        
        self.superview?.bringSubviewToFront(self)
        
        if self.alpha == 0 {
            animation {
                self.alpha = 1
            }
        }
    }
    
    func hideSelf() {
        
        if self.alpha == 1 {
            animation {
                self.alpha = 0
            }
        }
        
//        indexTag -= 1
        
//        if indexTag == 0 {
//
//            if self.alpha == 1 {
//                animation {
//                    self.alpha = 0
//                }
//            }
//        }
    }
}
